package com.xdjd.mobile.business.service;

import com.xdjd.mobile.business.constant.TradeChannelEnum;
import com.xdjd.mobile.business.dao.daoservice.member.MemberExchangeRecordPoService;
import com.xdjd.mobile.business.dao.daoservice.member.MemberTradeRecordPoService;
import com.xdjd.mobile.business.entity.model.member.MemberExchangeRecordPo;
import com.xdjd.mobile.business.entity.model.member.MemberTradeRecordPo;
import com.xdjd.mobile.business.entity.param.trade.TradeParam;
import com.xdjd.mobile.business.entity.result.trade.ExchangeRecordResult;
import com.xdjd.mobile.business.entity.result.trade.TradeRecordResult;
import com.xdjd.mobile.business.util.DateUtil;
import net.oschina.durcframework.easymybatis.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TradeService {

    @Autowired
    private MemberTradeRecordPoService memberTradeRecordPoService;
    @Autowired
    private MemberExchangeRecordPoService memberExchangeRecordPoService;

    public PageInfo<TradeRecordResult> getTradeRecordList(TradeParam param, Long memberId) {
        PageInfo<MemberTradeRecordPo> tradeRecordPage = memberTradeRecordPoService.getTradeRecordList(param.getRecordType(), memberId, param.getPageIndex(), param.getPageSize());
        PageInfo<TradeRecordResult> pageInfo = new PageInfo<>();
        List<MemberTradeRecordPo> list = tradeRecordPage.getList();
        List<TradeRecordResult> resultList = new ArrayList<>();
        for (MemberTradeRecordPo recordPo : list) {
            TradeRecordResult result = new TradeRecordResult();
            result.setRecordNo(recordPo.getRecordNo());
            result.setAmount(recordPo.getAmount());
            result.setChannel(TradeChannelEnum.getByCode(recordPo.getChannel().intValue()).getDesc());
            result.setTradeDate(DateUtil.getDate(recordPo.getRecordDate()));
            result.setStatus(recordPo.getStatus());
            resultList.add(result);
        }
        pageInfo.setTotal(tradeRecordPage.getTotal());
        pageInfo.setPageIndex(tradeRecordPage.getPageIndex());
        pageInfo.setPageSize(tradeRecordPage.getPageSize());
        pageInfo.setList(resultList);
        return pageInfo;
    }

    public PageInfo<ExchangeRecordResult> getExchangeRecordList(Integer pageIndex, Integer pageSize, Long memberId) {
        PageInfo<MemberExchangeRecordPo> tradeRecordPage = memberExchangeRecordPoService.getExchangeRecordList(memberId, pageIndex, pageSize);
        PageInfo<ExchangeRecordResult> pageInfo = new PageInfo<>();
        List<MemberExchangeRecordPo> list = tradeRecordPage.getList();
        List<ExchangeRecordResult> resultList = new ArrayList<>();
        for (MemberExchangeRecordPo recordPo : list) {
            ExchangeRecordResult result = new ExchangeRecordResult();
            result.setRecordNo(recordPo.getRecordNo());
            result.setPrizeName(recordPo.getPrizeName());
            result.setPrizeLogo(recordPo.getPrizeLogo());
            result.setExchangeDate(DateUtil.getDate(recordPo.getExchangeDate()));
            result.setStatus(recordPo.getStatus());
            resultList.add(result);
        }
        pageInfo.setTotal(tradeRecordPage.getTotal());
        pageInfo.setPageSize(tradeRecordPage.getPageSize());
        pageInfo.setPageIndex(tradeRecordPage.getPageIndex());
        pageInfo.setList(resultList);
        return pageInfo;
    }
}
